<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/page/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/page/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/page/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/page/images/logo.svg" color="#222">

<link rel="stylesheet" href="/page/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"assd12138.gitee.io","root":"/page/","images":"/page/images","scheme":"Gemini","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/page/search.json","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/page/js/config.js"></script>
<meta name="description" content="尝试使用Vue3的新语法-1，新的特性">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue3新内容-1">
<meta property="og:url" content="https://assd12138.gitee.io/page/2020/09/18/Vue3%E6%96%B0%E5%86%85%E5%AE%B9-1/index.html">
<meta property="og:site_name" content="爱爱爱呵风他">
<meta property="og:description" content="尝试使用Vue3的新语法-1，新的特性">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-09-18T05:43:12.000Z">
<meta property="article:modified_time" content="2022-05-09T03:18:13.062Z">
<meta property="article:author" content="爱爱爱呵风他">
<meta property="article:tag" content="Vue">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://assd12138.gitee.io/page/2020/09/18/Vue3%E6%96%B0%E5%86%85%E5%AE%B9-1/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://assd12138.gitee.io/page/2020/09/18/Vue3%E6%96%B0%E5%86%85%E5%AE%B9-1/","path":"2020/09/18/Vue3新内容-1/","title":"Vue3新内容-1"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue3新内容-1 | 爱爱爱呵风他</title>
  




  <noscript>
    <link rel="stylesheet" href="/page/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/page/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">爱爱爱呵风他</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/page/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/page/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-archives"><a href="/page/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Computed%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E7%9A%84set%E5%92%8Cget"><span class="nav-number">1.</span> <span class="nav-text">Computed计算属性的set和get</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Teleport%E9%80%8F%E4%BC%A0%E6%A8%A1%E6%9D%BF"><span class="nav-number">2.</span> <span class="nav-text">Teleport透传模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%87%E6%AE%B5-%E5%A4%9A%E6%A0%B9%E7%BB%84%E4%BB%B6"><span class="nav-number">3.</span> <span class="nav-text">片段(多根组件)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E4%BA%8B%E4%BB%B6%E7%9A%84%E4%BC%A0%E5%80%BC%E9%AA%8C%E8%AF%81"><span class="nav-number">4.</span> <span class="nav-text">自定义事件的传值验证</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="爱爱爱呵风他"
      src="https://s1.ax1x.com/2020/08/26/dWnF58.jpg">
  <p class="site-author-name" itemprop="name">爱爱爱呵风他</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/page/archives/">
          <span class="site-state-item-count">24</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/page/tags/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/vocabulary@2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://assd12138.gitee.io/page/2020/09/18/Vue3%E6%96%B0%E5%86%85%E5%AE%B9-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s1.ax1x.com/2020/08/26/dWnF58.jpg">
      <meta itemprop="name" content="爱爱爱呵风他">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱爱爱呵风他">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue3新内容-1
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-18 13:43:12" itemprop="dateCreated datePublished" datetime="2020-09-18T13:43:12+08:00">2020-09-18</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-09 11:18:13" itemprop="dateModified" datetime="2022-05-09T11:18:13+08:00">2022-05-09</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>尝试使用Vue3的新语法-1，新的特性</p>
<span id="more"></span>

<script src="https://cdn.jsdelivr.net/npm/vue@3.0.0-rc.12/dist/vue.global.min.js"></script>


<h3 id="Computed计算属性的set和get"><a href="#Computed计算属性的set和get" class="headerlink" title="Computed计算属性的set和get"></a>Computed计算属性的set和get</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">set-computed</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">  <span class="keyword">const</span> app1 = <span class="title class_">Vue</span>.<span class="title function_">createApp</span>(&#123;&#125;)</span></span><br><span class="line"><span class="language-javascript">  app1.<span class="title function_">component</span>(<span class="string">&#x27;SetComputed&#x27;</span>,</span></span><br><span class="line"><span class="language-javascript">    &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="title function_">data</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="language-javascript">          <span class="attr">num1</span>: <span class="number">0</span>,</span></span><br><span class="line"><span class="language-javascript">          <span class="attr">num2</span>: <span class="number">2</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">      &#125;,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">computed</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="attr">val</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">          <span class="title function_">get</span>(<span class="params"></span>) &#123;<span class="comment">// get方法，和Vue2中的计算属性一致</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">num1</span> - <span class="variable language_">this</span>.<span class="property">num2</span>;</span></span><br><span class="line"><span class="language-javascript">          &#125;,</span></span><br><span class="line"><span class="language-javascript">          <span class="title function_">set</span>(<span class="params">data</span>) &#123;<span class="comment">// set方法，当使用this.val=xxx时,xxx会传递给data，进行一系列的操作</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">const</span> arr = data.<span class="title function_">split</span>(<span class="string">&#x27;-&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">this</span>.<span class="property">num1</span> = <span class="title class_">Number</span>.<span class="built_in">parseInt</span>(arr[<span class="number">0</span>]);</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">this</span>.<span class="property">num2</span> = <span class="title class_">Number</span>.<span class="built_in">parseInt</span>(arr[<span class="number">1</span>]);</span></span><br><span class="line"><span class="language-javascript">          &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">      &#125;,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">template</span>: <span class="string">`</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">      &lt;div&gt;&#123;&#123;val&#125;&#125;&lt;/div&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">      &lt;button @click=&#x27;change&#x27;&gt;change&lt;/button&gt;`</span></span></span><br><span class="line"><span class="language-javascript">      ,</span></span><br><span class="line"><span class="language-javascript">      <span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">change</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">          <span class="variable language_">this</span>.<span class="property">val</span> = <span class="string">&#x27;9-1&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;)</span></span><br><span class="line"><span class="language-javascript">  app1.<span class="title function_">mount</span>(<span class="string">&#x27;#app1&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>代码示例</p>
<hr>

<div id="app1">
    <set-computed />
</div>
<script>
  const app1 = Vue.createApp({})
  app1.component('SetComputed',
    {
      data() {
        return {
          num1: 0,
          num2: 2
        }
      },
      computed: {
        val: {
          get() {
            return this.num1 - this.num2;
          },
          set(data) {
            const arr = data.split('-');
            this.num1 = Number.parseInt(arr[0]);
            this.num2 = Number.parseInt(arr[1]);
          }
        }
      },
      template: `
      <div>{{val}}</div>
      <button @click='change'>change</button>`
      ,
      methods: {
        change() {
          this.val = '9-1';
        }
      }
    })
  app1.mount('#app1')
</script>

<hr>
<h3 id="Teleport透传模板"><a href="#Teleport透传模板" class="headerlink" title="Teleport透传模板"></a>Teleport透传模板</h3><p>如果要显示一个对话框，通常要在DOM底部增加一个modal元素，因为不需要处理上下的dom结构，比较方便。Teleport提供了一个方法让组件能将一个html片段注入到Vue控制范围之外的地方。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app2&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">teleport-demo</span>&gt;</span><span class="tag">&lt;/<span class="name">teleport-demo</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 预留的底部容器 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">footer</span> <span class="attr">id</span>=<span class="string">&quot;app2-foot-wrapper&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">footer</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-handlebars"><span class="language-xml"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  const app2 = Vue.createApp(&#123;&#125;)</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  app2.component(&#x27;TeleportDemo&#x27;,</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      data() &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        return &#123;show: false,data:&#x27;我是对话框内容!!!!&#x27;&#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      &#125;,</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      methods:</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        change()&#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">          this.show = !this.show;&#125;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      &#125;,</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      template :</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      `</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&#x27;change&#x27;</span>&gt;</span>显示下面的透传<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        <span class="tag">&lt;<span class="name">teleport</span> <span class="attr">to</span>=<span class="string">&quot;#app2-foot-wrapper&quot;</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">&quot;show&quot;</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            <span class="tag">&lt;<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">              </span><span class="template-variable">&#123;&#123;<span class="name">data</span>&#125;&#125;</span><span class="language-xml"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">        <span class="tag">&lt;/<span class="name">teleport</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span>`</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    &#125;)</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  app2.mount(&#x27;#app2&#x27;)</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：只能注入到Vue实例控制之外的地方，<strong>wrapper放在实例内就无法注入</strong></p>
</blockquote>
<blockquote>
<p>尽管被注入到了Vue实例控制之外的地方，teleport中的内容仍然隶属于其父组件，如果注入的内容是另一个组件，父组件可以向它传props</p>
</blockquote>
<hr>
<p>示例:红框是预留了用来注入的，不包含在Vue实例中</p>

<div id="app2">
    <teleport-demo></teleport-demo>
  </div>
  <!-- 预留的底部容器 -->
<footer id="app2-foot-wrapper"></footer>
<style>
  #app2-foot-wrapper{
    border: 1px solid red;
  }
</style>
<script>
  const app2 = Vue.createApp({})
  app2.component('TeleportDemo',
    {
      data() {
        return {show: false,data:'我是对话框内容!!!!'}
      },
      methods:
      {
        change(){
          this.show = !this.show;}
      },
      template :
      `
      <div>
        <button @click='change'>显示下面的透传</button>
        <teleport to="#app2-foot-wrapper">
          <div v-if="show">
            <div>
              {{data}}
            </div>
          </div>
        </teleport>
      </div>`
    })
  app2.mount('#app2')
</script>


<hr>
<h3 id="片段-多根组件"><a href="#片段-多根组件" class="headerlink" title="片段(多根组件)"></a>片段(多根组件)</h3><p>在Vue2.x中，所有的组件都必须有一个根组件，这样在使用这个组件的时候将所有的属性合并应用到根组件上去，尤其是一些侦听器/class/id/style之类的属性，都是作用在组件的根组件上的。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">header</span>&gt;</span>...<span class="tag">&lt;/<span class="name">header</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">main</span>&gt;</span>...<span class="tag">&lt;/<span class="name">main</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">footer</span>&gt;</span>...<span class="tag">&lt;/<span class="name">footer</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在Vue3.0中，一个组件可以不必有根组件（这时称这种组件为片段），但是在这种情况发生时，必须将传过来的属性绑定在一个指定的元素上</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 直接上示例 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app3&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">fragment-demo</span> <span class="attr">class</span>=<span class="string">&quot;app3Red&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">  <span class="selector-class">.app3Red</span>&#123;</span></span><br><span class="line"><span class="language-css">    <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="language-css">  &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-handlebars"><span class="language-xml"></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  const app3 = Vue.createApp(&#123;&#125;)</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  app3.component(&#x27;FragmentDemo&#x27;, &#123;</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    template:</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    `</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    <span class="tag">&lt;<span class="name">p</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">   <span class="tag">&lt;<span class="name">span</span>&gt;</span>第1段<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  <span class="tag">&lt;<span class="name">p</span> <span class="attr">v-bind</span>=<span class="string">&quot;$attrs&quot;</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    <span class="tag">&lt;<span class="name">span</span>&gt;</span>第2段<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  <span class="tag">&lt;<span class="name">p</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">    <span class="tag">&lt;<span class="name">span</span>&gt;</span>第3段<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  <span class="tag">&lt;/<span class="name">p</span>&gt;</span>`,</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  &#125;)</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars">  app3.mount(&#x27;#app3&#x27;)</span></span></span><br><span class="line"><span class="language-xml"><span class="language-handlebars"></span></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>看下面的示例，由于手动将$attrs绑定到了第2段上，因此，只有第2段的dom上有class=”app3Red”的这个属性并且变红</p>
<hr>

<div id="app3">
  <fragment-demo class="app3Red" />
</div>
<style>
  .app3Red{
    color: red;
  }
</style>
<script>
  const app3 = Vue.createApp({})
  app3.component('FragmentDemo', {
    template:
    `
    <p>
   <span>第1段</span>
  </p>
  <p v-bind="$attrs">
    <span>第2段</span>
  </p>
  <p>
    <span>第3段</span>
  </p>`,
  })
  app3.mount('#app3')
</script>


<hr>
<h3 id="自定义事件的传值验证"><a href="#自定义事件的传值验证" class="headerlink" title="自定义事件的传值验证"></a>自定义事件的传值验证</h3><p>类似<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/guide/components-props.html#Prop-%E9%AA%8C%E8%AF%81">prop的传值验证</a>,自定义事件的参数也支持传值验证发出警告（无法阻止事件发生）<br>将自定义事件写在子组件的emits字段中作为一个函数，这个函数就是验证函数</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app4&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">father</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">son</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">father</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">  <span class="keyword">const</span> app4 = <span class="title class_">Vue</span>.<span class="title function_">createApp</span>(&#123;&#125;)</span></span><br><span class="line"><span class="language-javascript">  app4.<span class="title function_">component</span>(<span class="string">&#x27;Father&#x27;</span>, &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">data</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="attr">value</span>: <span class="string">&#x27;app&#x27;</span></span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="title function_">getStr</span>(<span class="params">str</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.<span class="property">value</span> = str</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">template</span>:</span></span><br><span class="line"><span class="language-javascript">      <span class="string">`&lt;div&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">        &lt;div&gt;我只愿意接受子组件的以\&#x27;a\&#x27;开头的字符串:&#123;&#123;value&#125;&#125;&lt;/div&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">        &lt;Son @my-event=&quot;getStr&quot; /&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">    &lt;/div&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">    `</span></span></span><br><span class="line"><span class="language-javascript">  &#125;)</span></span><br><span class="line"><span class="language-javascript">  app4.<span class="title function_">component</span>(<span class="string">&#x27;Son&#x27;</span>, &#123;</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">emits</span>: &#123; <span class="comment">// 验证函数全都写在这里面</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">      <span class="string">&#x27;my-event&#x27;</span>: <span class="function">(<span class="params">str</span>) =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> pattern = <span class="regexp">/^a/</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (!pattern.<span class="title function_">test</span>(str)) &#123;</span></span><br><span class="line"><span class="language-javascript">          <span class="title function_">alert</span>(<span class="string">&#x27;warning&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">          <span class="keyword">return</span> <span class="literal">false</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> <span class="literal">true</span>;</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">data</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="keyword">return</span> &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="attr">value</span>: <span class="string">&#x27;&#x27;</span>,</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">methods</span>: &#123;</span></span><br><span class="line"><span class="language-javascript">      <span class="title function_">send</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="variable language_">this</span>.$emit(<span class="string">&#x27;my-event&#x27;</span>, <span class="variable language_">this</span>.<span class="property">value</span>)</span></span><br><span class="line"><span class="language-javascript">      &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;,</span></span><br><span class="line"><span class="language-javascript">    <span class="attr">template</span>:</span></span><br><span class="line"><span class="language-javascript">      <span class="string">`&lt;div&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">          &lt;input v-model=&quot;value&quot; /&gt;&lt;button @click=&quot;send&quot;&gt;发送&lt;/button&gt;</span></span></span><br><span class="line"><span class="string"><span class="language-javascript">    &lt;/div&gt;`</span></span></span><br><span class="line"><span class="language-javascript">  &#125;)</span></span><br><span class="line"><span class="language-javascript">  app4.<span class="title function_">mount</span>(<span class="string">&#x27;#app4&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<hr>

<div id="app4">
    <father>
      <son/>
    </father>
</div>
<script>
  const app4 = Vue.createApp({})
  app4.component('Father', {
    data() {
      return {
        value: 'app'
      }
    },
    methods: {
      getStr(str) {
        this.value = str
      }
    },
    template:
      `<div>
        <div>我只愿意接受子组件的以\'a\'开头的字符串:{{value}}</div>
        <Son @my-event="getStr" />
    </div>
    `
  })
  app4.component('Son', {
    emits: { // 验证函数

      'my-event': (str) => {
        const pattern = /^a/
        if (!pattern.test(str)) {
          alert('warning')
          return false;
        }
        return true;
      }
    },

    data() {
      return {
        value: '',
      }
    },
    methods: {
      send() {
        this.$emit('my-event', this.value)
      }
    },
    template:
      `<div>
          <input v-model="value" /><button @click="send">发送</button>
    </div>`
  })
  app4.mount('#app4')
</script>


<hr>
<blockquote>
<p>思考：上面这个例子，如果要阻止事件的发生，是否可以在验证函数中再触发一个$emit(‘prevent-event’),并在父组件监听设置一个标志，如果标志为false，那么就取消消息的接收</p>
</blockquote>
<blockquote>
<p>不行，在验证函数中，this指向了window，如果要阻止发生只能将验证的流程写在父组件的处理函数中</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/page/tags/Vue/" rel="tag"># Vue</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/page/2020/09/11/%E7%AC%94%E8%AF%95%E9%A2%98%E6%B1%87%E6%80%BB/" rel="prev" title="笔试题汇总">
                  <i class="fa fa-chevron-left"></i> 笔试题汇总
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/page/2020/09/19/Vue3%E6%96%B0%E5%86%85%E5%AE%B9-2/" rel="next" title="Vue3新内容-2">
                  Vue3新内容-2 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">爱爱爱呵风他</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/page/js/comments.js"></script><script src="/page/js/utils.js"></script><script src="/page/js/motion.js"></script><script src="/page/js/next-boot.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/page/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"assd12138/utterancesComment","issue_term":"pathname","theme":"preferred-color-scheme"}</script>
<script src="/page/js/third-party/comments/utterances.js"></script>

</body>
</html>
